package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TDingtalkMessage;
import com.ruoyi.system.domain.TWorkOrder;
import com.ruoyi.system.service.IDingTalkClientService;
import com.ruoyi.system.service.ITDingtalkMessageService;
import com.ruoyi.system.service.ITWorkOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * 工单执行列Controller
 * 
 * @author wuym
 * @date 2021-04-24
 */
@Controller
@Api(value="工单执行列表管理")
@RequestMapping("/system/order")
public class TWorkOrderController extends BaseController
{
    private String prefix = "system/order";

    @Value("${cloudpivot.message.template.workOrderMsg}")
    private String workOrderMsg;

    @Autowired
    private ITWorkOrderService tWorkOrderService;
    @Autowired
    private ITDingtalkMessageService itDingtalkMessageService;

    @Autowired
    private IDingTalkClientService dingTalkClientService;

    @RequiresPermissions("system:order:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/order";
    }
    @Autowired
    private SimpMessagingTemplate messagingTemplate; // 通过messagingTemplate向浏览器发送消息
    /**
     * 查询工单执行列列表
     */
    //@RequiresPermissions("system:order:list")
    @ApiOperation("获取工单执行列表")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TWorkOrder tWorkOrder)
    {
        startPage();
        List<TWorkOrder> list = tWorkOrderService.selectTWorkOrderList(tWorkOrder);
        return getDataTable(list);
    }

    /**
     * 导出工单执行列列表
     */
    @RequiresPermissions("system:order:export")
    @Log(title = "工单执行列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TWorkOrder tWorkOrder)
    {
        List<TWorkOrder> list = tWorkOrderService.selectTWorkOrderList(tWorkOrder);
        ExcelUtil<TWorkOrder> util = new ExcelUtil<TWorkOrder>(TWorkOrder.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 新增工单执行列
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存工单执行列
     */
    //@RequiresPermissions("system:order:add")
    @ApiOperation("工单推送接口")
    @Log(title = "工单执行列", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TWorkOrder tWorkOrder)
    {
        //新增工单并发送钉钉消息
        TWorkOrder tWorkOrderDB = tWorkOrderService.selectTWorkOrderByMesNo(tWorkOrder.getMesNo());
        if(tWorkOrderDB == null){
            //新增记录
            tWorkOrder.setCreateTime(new Date());
            tWorkOrderService.insertTWorkOrder(tWorkOrder);
        }else {
            //更新记录
            tWorkOrder.setUpdateTime(new Date());
            tWorkOrderService.updateTWorkOrder(tWorkOrder);
        }
        //增加钉钉消息记录
        TDingtalkMessage message = new TDingtalkMessage();
        message.copyWorkOrder(tWorkOrder);
        message.setStatus(0);//待通知
        message.setCreateTime(new Date());
        itDingtalkMessageService.insertTDingtalkMessage(message);
        //调用钉钉消息通知
        String result = dingTalkClientService.sendTextMessageToDingTalk(message.getOperationNo(), MessageFormat.format(workOrderMsg, message.getOrderNo(), message.getWorkStatus()));
        //钉钉消息状态更新
        if(StringUtils.isNotBlank(result) && "ok".equals(result)){
            //更新通知状态为：已通知
            message.setStatus(1);//已通知
            itDingtalkMessageService.updateTDingtalkMessage(message);
        }
        return toAjax(true);
    }
    /**
     * 按工单状态统计数据
     * @return
     */
    @ApiOperation("按工单状态统计")
    @Log(title = "工单执行列", businessType = BusinessType.OTHER)
    @PostMapping("/countByWorkStatus")
    @ResponseBody
    public AjaxResult countByWorkStatus(){
        Map<String, Integer> data = tWorkOrderService.countByWorkStatus();
        return new AjaxResult(AjaxResult.Type.SUCCESS, "success", data);
    }
    /**
     * 获取班组列表
     * @return
     */
    @ApiOperation("获取班组列表")
    @Log(title = "获取班组列表", businessType = BusinessType.OTHER)
    @GetMapping("/getWorkGroupNames")
    @ResponseBody
    public AjaxResult getWorkGroupNames(){
        List<String> data = tWorkOrderService.getWorkGroupNames();
        return new AjaxResult(AjaxResult.Type.SUCCESS, "success", data);
    }

    /**
     * 修改工单执行列
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TWorkOrder tWorkOrder = tWorkOrderService.selectTWorkOrderById(id);
        mmap.put("tWorkOrder", tWorkOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存工单执行列
     */
    @RequiresPermissions("system:order:edit")
    @Log(title = "工单执行列", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TWorkOrder tWorkOrder)
    {
        return toAjax(tWorkOrderService.updateTWorkOrder(tWorkOrder));
    }

    /**
     * 删除工单执行列
     */
    @RequiresPermissions("system:order:remove")
    @Log(title = "工单执行列", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tWorkOrderService.deleteTWorkOrderByIds(ids));
    }
}
